package com.shop2cn.iapi.subscription.testcase;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import org.junit.After;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.shop2cn.iapi.subscription.parameter.*;
import com.shop2cn.iapi.subscription.service.*;
import com.sun.org.apache.bcel.internal.generic.NEW;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.springframework.context.annotation.Bean;

import com.ymatou.iapi.godmch.parameter.createIndepMchBean;
import com.ymatou.iapi.godmch.service.createIndepMchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.GodmchSubscriptionService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmch.funcpackage;
import com.ymttest.database.model.godmch.funcpackageExample;
import com.ymttest.database.sqlwapper.godmch.funcpackageWapper;
import com.ymttest.utils.TestDataManager;

/**
*查询订购和套餐信息Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetSubscription{
	private static GetSubscriptionBean getsubscriptionBean;
	private static GetSubscriptionCall getsubscriptionCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询订购和套餐信息Test");
	}

	@Before
	public void caseUp() {
		getsubscriptionBean=new GetSubscriptionBean();
		getsubscriptionCall=new GetSubscriptionCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getsubscriptionCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSubscription_001() {
		Logger.start(true,"未订购商户查询直播，成功");
		try {
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			getsubscriptionBean.setMchId(mchId);
			getsubscriptionBean.setFuncType(0);
			getsubscriptionCall.setData(getsubscriptionBean);
			getsubscriptionCall.callService();			
			Logger.verifyEquals("200", getsubscriptionCall.getString("code"), "验证code");
			
			JsonObject data = getsubscriptionCall.getJsonObject().get("data").getAsJsonObject();
			JsonObject subscriptionFunc = data.get("subscriptionFunc").getAsJsonObject();
			Logger.verifyEquals(0,subscriptionFunc.get("subscriptionStatus").getAsInt(), "验证subscriptionStatus");
			Logger.verifyEquals(true,subscriptionFunc.get("subscriptionExpirationTime").isJsonNull(), "验证subscriptionExpirationTime");
			Logger.verifyEquals(0,subscriptionFunc.get("availableDay").getAsInt(), "验证availableDay");
			Logger.verifyEquals(true,subscriptionFunc.get("remind").isJsonNull(), "验证remind");
			Logger.verifyEquals(false, subscriptionFunc.get("tryout").getAsBoolean(), "验证tryout");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSubscription_002() {
		Logger.start(true,"直播试用商户查询，成功");
		try {
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			GodmchSubscriptionService.applyTryout(mchId, 0, 1);
			getsubscriptionBean.setMchId(mchId);
			getsubscriptionBean.setFuncType(0);
			getsubscriptionCall.setData(getsubscriptionBean);
			getsubscriptionCall.callService();			
			Logger.verifyEquals("200", getsubscriptionCall.getString("code"), "验证code");
			
			JsonObject data = getsubscriptionCall.getJsonObject().get("data").getAsJsonObject();
			JsonObject subscriptionFunc = data.get("subscriptionFunc").getAsJsonObject();
			Logger.verifyEquals(1,subscriptionFunc.get("subscriptionStatus").getAsInt(), "验证subscriptionStatus");
			Logger.verifyEquals(29,subscriptionFunc.get("availableDay").getAsInt(), "验证availableDay");
			Logger.verifyIsContains("不足30天",subscriptionFunc.get("remind").getAsString(), "验证remind");
			Logger.verifyEquals(true, subscriptionFunc.get("tryout").getAsBoolean(), "验证tryout");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore // 未启用进销存
	public void Tc_GetSubscription_003() {
		Logger.start(true,"进销存试用商户查询，成功");
		try {
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			GodmchSubscriptionService.applyTryout(mchId, 1, 1);
			getsubscriptionBean.setMchId(mchId);
			getsubscriptionBean.setFuncType(1);
			getsubscriptionCall.setData(getsubscriptionBean);
			getsubscriptionCall.callService();			
			Logger.verifyEquals("200", getsubscriptionCall.getString("code"), "验证code");
			
			JsonObject data = getsubscriptionCall.getJsonObject().get("data").getAsJsonObject();
			JsonObject subscriptionFunc = data.get("subscriptionFunc").getAsJsonObject();
			Logger.verifyEquals(1,subscriptionFunc.get("subscriptionStatus").getAsInt(), "验证subscriptionStatus");
			Logger.verifyEquals(29,subscriptionFunc.get("availableDay").getAsInt(), "验证availableDay");
			Logger.verifyIsContains("不足30天",subscriptionFunc.get("remind").getAsString(), "验证remind");
			Logger.verifyEquals(true, subscriptionFunc.get("tryout").getAsBoolean(), "验证tryout");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore // 未启用独立小程序
	public void Tc_GetSubscription_004() {
		Logger.start(true,"独立小程序试用商户查询，成功");
		try {
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			GodmchSubscriptionService.applyTryout(mchId, 2, 1);
			getsubscriptionBean.setMchId(mchId);
			getsubscriptionBean.setFuncType(2);
			getsubscriptionCall.setData(getsubscriptionBean);
			getsubscriptionCall.callService();			
			Logger.verifyEquals("200", getsubscriptionCall.getString("code"), "验证code");
			
			JsonObject data = getsubscriptionCall.getJsonObject().get("data").getAsJsonObject();
			JsonObject subscriptionFunc = data.get("subscriptionFunc").getAsJsonObject();
			Logger.verifyEquals(1,subscriptionFunc.get("subscriptionStatus").getAsInt(), "验证subscriptionStatus");
			Logger.verifyEquals(29,subscriptionFunc.get("availableDay").getAsInt(), "验证availableDay");
			Logger.verifyIsContains("不足30天",subscriptionFunc.get("remind").getAsString(), "验证remind");
			Logger.verifyEquals(true, subscriptionFunc.get("tryout").getAsBoolean(), "验证tryout");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSubscription_005() {
		Logger.start(true,"商户号不存在");
		try {
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			
			getsubscriptionBean.setMchId(mchId+10);
			getsubscriptionBean.setFuncType(0);
			getsubscriptionCall.setData(getsubscriptionBean);
			getsubscriptionCall.callService();			
			Logger.verifyEquals("200", getsubscriptionCall.getString("code"), "验证code");
			
			JsonObject data = getsubscriptionCall.getJsonObject().get("data").getAsJsonObject();
			JsonObject subscriptionFunc = data.get("subscriptionFunc").getAsJsonObject();
			
			Logger.verifyEquals(0,subscriptionFunc.get("subscriptionStatus").getAsInt(), "验证subscriptionStatus");
			Logger.verifyEquals(true,subscriptionFunc.get("subscriptionExpirationTime").isJsonNull(), "验证subscriptionExpirationTime");
			Logger.verifyEquals(0,subscriptionFunc.get("availableDay").getAsInt(), "验证availableDay");
			Logger.verifyIsContains(true,subscriptionFunc.get("remind").isJsonNull(), "验证remind");
			Logger.verifyEquals(false, subscriptionFunc.get("tryout").getAsBoolean(), "验证tryout");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSubscription_006() {
		Logger.start(true,"上级开通分销，下级自动开通");
		try {
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			Integer DistributorId = GodmchManageCallService.CreateDistributor(mchId);
			GodmchManageCallService.SetTrueMchChargeConfig(mchId, Arrays.asList(0,1,2,3,4),true);
			JsonObject applyCharge = GodmchManageCallService.ApplyCharge(mchId, 3, 6);
			
			getsubscriptionBean.setMchId(DistributorId);
			getsubscriptionBean.setFuncType(3);
			getsubscriptionCall.setData(getsubscriptionBean);
			getsubscriptionCall.callService();			
			Logger.verifyEquals("200", getsubscriptionCall.getString("code"), "验证code");
			JsonObject data = getsubscriptionCall.getJsonObject().get("data").getAsJsonObject();
			JsonObject subscriptionFunc = data.get("subscriptionFunc").getAsJsonObject();
			Logger.verifyEquals(1,subscriptionFunc.get("subscriptionStatus").getAsInt(), "验证subscriptionStatus");
			Logger.verifyEquals(29,subscriptionFunc.get("availableDay").getAsInt(), "验证availableDay");
			Logger.verifyIsContains("不足30天",subscriptionFunc.get("remind").getAsString(), "验证remind");
			Logger.verifyEquals(true, subscriptionFunc.get("tryout").getAsBoolean(), "验证tryout");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSubscription_007() {
		Logger.start(true,"未订购商户查询直播-type5，成功");
		try {
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			getsubscriptionBean.setMchId(mchId);
			getsubscriptionBean.setFuncType(50);
			getsubscriptionCall.setData(getsubscriptionBean);
			getsubscriptionCall.callService();			
			Logger.verifyEquals("200", getsubscriptionCall.getString("code"), "验证code");
			
			JsonObject data = getsubscriptionCall.getJsonObject().get("data").getAsJsonObject();
			JsonObject subscriptionFunc = data.get("subscriptionFunc").getAsJsonObject();
			Logger.verifyEquals(0,subscriptionFunc.get("subscriptionStatus").getAsInt(), "验证subscriptionStatus");
			Logger.verifyEquals(true,subscriptionFunc.get("subscriptionExpirationTime").isJsonNull(), "验证subscriptionExpirationTime");
			Logger.verifyEquals(0,subscriptionFunc.get("availableDay").getAsInt(), "验证availableDay");
			Logger.verifyEquals(true,subscriptionFunc.get("remind").isJsonNull(), "验证remind");
			Logger.verifyEquals(false, subscriptionFunc.get("tryout").getAsBoolean(), "验证tryout");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	//@Ignore // 未启用独立小程序
	public void Tc_GetSubscription_008() {
		Logger.start(true,"独立小程序试用商户查询，成功");
		try {
			//Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
			Integer mchId=createIndepMchCall.getmchId();
			GodmchSubscriptionService.applyTryout(mchId,0, 6);//试用根据funcPackageId 去找对应的funcType，所有接口中的funcType 无效
			getsubscriptionBean.setMchId(mchId);
			getsubscriptionBean.setFuncType(3);
			getsubscriptionCall.setData(getsubscriptionBean);
			getsubscriptionCall.callService();			
			Logger.verifyEquals("200", getsubscriptionCall.getString("code"), "验证code");
			
			funcpackageWapper funcpackageWapper=new funcpackageWapper();
			funcpackageExample example=new funcpackageExample();
			example.createCriteria().andFuncPackageIdEqualTo(6).andFuncTypeEqualTo(getsubscriptionBean.getFuncType());
			List<funcpackage> packages=funcpackageWapper.selectByExample(example);
			
			JsonObject data = getsubscriptionCall.getJsonObject().get("data").getAsJsonObject();
			JsonObject subscriptionFunc = data.get("subscriptionFunc").getAsJsonObject();
			Logger.verifyEquals(1,subscriptionFunc.get("subscriptionStatus").getAsInt(), "验证subscriptionStatus");
			
			Logger.verifyEquals(364,subscriptionFunc.get("availableDay").getAsInt(), "验证availableDay");
			
			JsonArray return_funcPackageList = data.get("funcPackageList").getAsJsonArray();
			for(int i=0;i<packages.size();i++)
			{
				funcpackage DB_packag=packages.get(i);
				JsonObject return_jsonObj=return_funcPackageList.get(i).getAsJsonObject();
				
				Logger.verifyEquals("\""+DB_packag.getFuncName()+"\"",return_jsonObj.get("funcName").toString(), "验证FuncName");
				Logger.verifyEquals(DB_packag.getPackageAmount(),new BigDecimal(return_jsonObj.get("packageAmount").toString()), "验证PackageAmount");
				Logger.verifyEquals(DB_packag.getIndepPackageAmount(),new BigDecimal(return_jsonObj.get("indepPackageAmount").toString()), "验证IndepPackageAmount");
			}
			
			
//			Logger.verifyIsContains("不足30天",subscriptionFunc.get("remind").getAsString(), "验证remind");
//			Logger.verifyEquals(true, subscriptionFunc.get("tryout").getAsBoolean(), "验证tryout");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	//@Ignore // 未启用独立小程序
	public void Tc_GetSubscription_009() {
		Logger.start(true,"独立小程序 申请商户查询，成功");
		try {
			//Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
			Integer mchId=createIndepMchCall.getmchId();
			GodmchSubscriptionService.ApplyCharge(mchId, 2, 3);
			
			getsubscriptionBean.setMchId(mchId);
			getsubscriptionBean.setFuncType(0);
			getsubscriptionCall.setData(getsubscriptionBean);
			getsubscriptionCall.callService();			
			Logger.verifyEquals("200", getsubscriptionCall.getString("code"), "验证code");
			
/*			JsonObject data = getsubscriptionCall.getJsonObject().get("data").getAsJsonObject();
			JsonObject subscriptionFunc = data.get("subscriptionFunc").getAsJsonObject();
			Logger.verifyEquals(1,subscriptionFunc.get("subscriptionStatus").getAsInt(), "验证subscriptionStatus");
			Logger.verifyEquals(29,subscriptionFunc.get("availableDay").getAsInt(), "验证availableDay");
			Logger.verifyIsContains("不足30天",subscriptionFunc.get("remind").getAsString(), "验证remind");
			Logger.verifyEquals(true, subscriptionFunc.get("tryout").getAsBoolean(), "验证tryout");*/
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}